<template>
	<view class="body" v-if="show">
		<view class="goods">
			<block v-for="(item,index) in goodsList" :key="index">
				<view class="item" v-if="item.refund_status == 0">
					<!-- 顶部信息 -->
					<view class="top">
						<!-- 商品详情 -->
						<view class="infor">
							<view class="img">
								<image :src="item.spec.spec_image==''? item.image:item.sku_image" mode=""></image>
							</view>
							<view class="cont">
								<view class="c-top">
									<view class="name">{{ item.goods_name }}</view>
									<view class="price"> <text>￥</text>{{ item.goods_price }}</view>
								</view>
								<view class="c-bottom">
									<view class="type">
										<view>{{ item.goods_attr ? item.goods_attr :'' }}</view>
									</view>
									<view class="num">x{{ item.total_num }}</view>
								</view>
							</view>
						</view>
						<!-- 描述相符 -->
						<view class="description">
							<view class="left">描述相符</view>
							<uni-rate v-model="item.goods_score" margin="4"
								@change="onChange($event,'description',index)" active-color="#E43D33" size="24" />
						</view>
					</view>

					<!-- 评价内容 -->
					<view class="center">
						<textarea cols="30" rows="10" :value="item.goods_infor" placeholder="请输入您对商品的看法..."
							@input="inforChange($event,index)"></textarea>
					</view>
					<!-- 上传照片 -->
					<view class="upload">
						<block v-for="(img,Lindex) in item.list" :key="Llist">
							<view class="upload-img">
								<image :src="img" mode=""></image>
								<uni-icons type="clear" size="20" color="red"
									@click="deleteImg(index,Lindex)"></uni-icons>
							</view>
						</block>
						<view class="add" v-if="item.list.length < 5" @click="uploadImage(5 - item.list.length,index)">+
						</view>
					</view>
				</view>
			</block>
		</view>

		<view class="bottom">
			<!-- 物流服务 -->
			<view class="logistics">
				<view class="title">物流服务</view>
				<uni-rate v-model="logiValue" margin="4" @change="onChange($event,'logistics')" active-color="#E43D33"
					size="24" />
			</view>
			<!-- 服务态度 -->
			<view class="attitude">
				<view class="title">服务态度</view>
				<uni-rate v-model="attiValue" margin="4" @change="onChange($event,'attitude')" active-color="#E43D33"
					size="24" />
			</view>
			<!-- 是否匿名 -->
			<view class="anonymity">
				<view class="an-left">
					<view class="an-title">
						<checkbox-group @change="change">
							<label>
								<checkbox value="cb" :checked="state" />匿名
							</label>
						</checkbox-group>
					</view>
					<view class="tips">匿名会隐藏头像和昵称</view>
				</view>
				<view class="btn">
					<button @click="publish()">发布评价</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				id: 0,
				goodsList: [], //商品列表
				list: [],
				logiValue: 0, //物流分
				attiValue: 0, //态度分
				state: false, //是否匿名
				pageId: 0, //当前的订单id
				pageIndex: 0, //当前订单的index,
			}
		},
		onLoad(options) {
			this.id = options.id
			this.getDetail()
		},
		methods: {
			getDetail() {
				let _this = this

				uni.showLoading({
					title: '加载中...',
					mask: true
				})

				_this.$api.get('mall.order/detail', {
					id: _this.id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						_this.detail = res.data.order
						let list = res.data.order.goods
						_this.goodsList = list.map(item => {
							item.list = []
							item.urlList = []
							item.goods_score = 0
							item.goods_infor = ''
							return item
						})
						_this.show = true
					} else {
						uni.showModal({
							title: '提示',
							content: res.msg,
							showCancel: false,
							cancelText: '',
							confirmText: '确认',
							success: res => {
								uni.navigateBack({
									delta: 1
								});
							},
							fail: () => {},
							complete: () => {}
						});
					}
				}, fail => {})
			},
			onChange(e, type, index) {
				switch (type) {
					case 'description':
						this.goodsList[index].goods_score = e.value
						break;
					case 'logistics':
						this.logiValue = e.value
						break;
					default:
						this.attiValue = e.value
				}
			},
			uploadImage(num, index) {
				let _this = this
				uni.chooseImage({
					count: num, // 图片数量
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: async (res) => {
						const tempFilePaths = res.tempFilePaths;
						for (let i = 0; i < tempFilePaths.length; i++) {
							let result = await this.uploadFile(tempFilePaths[i])
							_this.goodsList[index].list.push(result.fullurl)
							_this.goodsList[index].urlList.push(result.url)
						}
					}
				});
			},
			change() {
				this.state = !this.state
			},
			uploadFile(tempFilePaths) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: this.baseUrl + 'common/upload', //上传图片api
						filePath: tempFilePaths,
						name: 'file',
						header: {
							token: uni.getStorageSync('app_token')
						},
						success: (res) => {
							let group = JSON.parse(res.data)
							uni.showToast({
								title: "上传成功",
								icon: "success"
							})
							resolve({
								url: group.data.url,
								fullurl: group.data.fullurl
							})
						}
					});
				})
			},
			deleteImg(index, Lindex) {
				this.goodsList[index].list.splice(Lindex, 1)
				this.goodsList[index].urlList.splice(Lindex, 1)
			},
			inforChange(e, index) {
				this.goodsList[index].goods_infor = e.detail.value
			},
			publish() {
				let _this = this
				_this.list = []
				let score = []
				_this.goodsList.forEach(item => {
					if (item.refund_status == 0) {
						_this.list.push({
							"content": item.goods_infor,
							"images": item.urlList.toString(','),
							"scores": item.goods_score,
							"id": item.id
						})
						score.push(item.goods_score)
					}
				})
				let list = JSON.stringify(_this.list)
				if (!score.includes(0) && _this.logiValue != 0 && _this.attiValue != 0) {
					uni.showLoading({
						title: '提交中...',
						mask: true
					})
					_this.$api.post('mall.evaluate/add', {
						order_id: _this.goodsList[0].order_id,
						delivery_service: _this.logiValue,
						service_attitude: _this.attiValue,
						is_anonymous: _this.state == true ? 1 : 0,
						order_goods_list: list
					}).then(res => {
						uni.hideLoading()
						if (res.code == 1) {
							uni.showModal({
								title: '提示',
								content: '发布成功',
								showCancel: false,
								success: res => {
									if (res.confirm) {
										uni.navigateBack({
											delta: 1
										})
									}
								}
							})
						} else {
							_this.toast(res.msg)
						}
					})
				} else {
					_this.toast('请完成所有评分')
				}

			}
		}
	}
</script>

<style>
	page {
		background-color: #F6F6F6;
		width: calc(100vw - 48rpx);
		padding: 0 24rpx 350rpx 24rpx;
	}

	.item {
		padding: 32rpx 32rpx 20rpx 32rpx;
		margin-top: 32rpx;
		background-color: #FFFFFF;
		box-shadow: 0rpx 2rpx 50rpx 0rpx rgba(0, 0, 0, 0.08);
		border-radius: 32rpx;
	}

	.item .infor,
	.c-top,
	.c-bottom,
	.description {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.cont {
		width: 80%;
	}

	.item .top {
		padding-bottom: 32rpx;
		border-bottom: 1rpx solid #E5E5E5;
	}

	.item .top .img {
		width: 100rpx;
		height: 100rpx;
		margin-right: 24rpx;
	}

	.item .img image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 8rpx;
	}

	.c-top .name {
		width: 70%;
		font-size: 28rpx;
		font-weight: bold;
		overflow: hidden;
		-webkit-line-clamp: 1;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		text-overflow: ellipsis;
		margin-bottom: 10rpx;
	}

	.c-top .price {
		font-size: 24rpx;
	}

	.c-top .price text {
		font-size: 20rpx;
	}

	.c-bottom {
		font-size: 20rpx;
		color: #999999;
	}

	.description {
		justify-content: flex-start;
		margin-top: 25rpx;
	}

	.description .left {
		margin-right: 30rpx;
		font-size: 24rpx;
		font-weight: bold;
		color: #666666;
	}

	/* 评价内容 */
	.center {
		padding-top: 40rpx;
		padding-left: 8rpx;
		font-size: 26rpx;
		color: #999999;
	}

	.center textarea {
		height: 190rpx !important;
	}

	/* 上传图片 */
	.upload {
		display: flex;
		flex-wrap: wrap;
		margin-top: 30rpx;
	}

	.upload .upload-img {
		width: 160rpx;
		height: 160rpx;
		position: relative;
		margin-right: 32rpx;
		margin-bottom: 20rpx;
	}

	.upload .upload-img .uni-icons {
		position: absolute;
		top: -18rpx;
		right: -18rpx;
		background-color: #FFFFFF;
		border-radius: 50%;
	}

	.upload image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
	}

	.upload .add {
		width: 160rpx;
		height: 160rpx;
		line-height: 160rpx;
		margin-bottom: 20rpx;
		font-size: 55rpx;
		color: rgba(0, 0, 0, 0.4);
		text-align: center;
		background: #F0F0F0;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
	}

	/* 底部信息 */
	.bottom {
		width: calc(100vw - 82rpx);
		padding: 32rpx 32rpx 32rpx 50rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		box-shadow: 0rpx 2rpx 50rpx 0rpx rgba(0, 0, 0, 0.08);
		z-index: 99;
	}

	.logistics,
	.attitude {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.logistics .title,
	.attitude .title {
		font-size: 24rpx;
		color: #666666;
		margin-right: 40rpx;
	}

	.anonymity {
		padding-top: 54rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	label {
		font-size: 24rpx;
		line-height: 40rpx;
		font-weight: bold;
	}

	.tips {
		font-size: 22rpx;
		color: #999999;
		margin-top: 10rpx;
	}

	.btn button {
		width: 343rpx;
		height: 90rpx;
		line-height: 89rpx;
		font-size: 28rpx;
		color: #FFFFFF;
		background: #000000;
		border-radius: 100rpx 100rpx 100rpx 100rpx;
	}

	.btn button::after {
		border: none;
	}

	checkbox .wx-checkbox-input {
		border-radius: 50%;
		width: 24rpx;
		height: 24rpx;
		border: 1px solid #ccc;
		background: #FFFFFF;
	}

	/* 复选框选中后样式 */
	checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		background: #000000;
		border: 1px solid #000000;
	}

	/*checkbox选中后图标样式  */
	checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
		width: 24rpx;
		height: 24rpx;
		line-height: 24rpx;
		text-align: center;
		font-size: 20rpx;
		color: #fff;
		background: transparent;
		transform: translate(-50%, -50%) scale(1);
		-webkit-transform: translate(-50%, -50%) scale(1);
	}
</style>